<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/17
 * Time: 21:38
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\common\business\PlaceOrder as BusObj;
use app\common\business\Wechat;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\lib\Time;
use think\App;
use think\Exception;

class Order extends Base
{
    protected BusObj $busObj;
    protected $indexWhere = [];
    protected $indexOrderBy = 'id desc';
    protected $indexField = '*';
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->busObj = new BusObj();
    }

    /**
     * @return string|\think\response\Json
     * @throws Exception
     */
    public function index()
    {
        $this->indexField = "*,theatre_id as GetTheatreIdData,openid as get_openid_user,id as get_detail_data_code";
        $nickname = $this->request->param('nickname', '', 'trim');
        $payStatus = $this->request->param('payStatus', 0, 'intval');
        $payState = $this->request->param('payState', 0, 'intval');
        $dtime = $this->request->param('dtime', '', 'trim');
        $theatre = $this->request->param('theatre', 0, 'intval');
        $theatreCate = $this->request->param('theatreCate', 0, 'intval');
        $venue = $this->request->param('venue', 0, 'intval');
        if (empty($dtime)) {
            $this->indexWhere[] = [
                ['create_time', 'between', Time::month()]
            ];
        } else {
            $time = explode('~', $dtime);
            $this->indexWhere[] = [
                ['create_time', 'between', [
                    strtotime($time[0]), strtotime($time[1])
                ]]
            ];
        }
        if (!empty($nickname)) {
            try {
                $wechat = (new Wechat())->getWechatByNickName($nickname);
            } catch (\Exception $e) {
                throw new Exception($e->getMessage());
            }
            $this->indexWhere[] = [
                ['openid', 'in', array_column($wechat, 'openid')]
            ];
        }
        if (!empty($payState)) {
            $this->indexWhere[] = [
                ['state', '=', $payState]
            ];
        }
        if (!empty($payStatus)) {
            if ($payStatus == 99) {
                $payStatus = 0;
            }
            $this->indexWhere[] = [
                ['payStatus', '=', $payStatus]
            ];
        }
        if (!empty($theatre)) {
            $this->indexWhere[] = [
                ['theatre_id', '=', $theatre]
            ];
        }
        if (!empty($theatreCate)) {
            $this->indexWhere[] = [
                ['theatreCate', '=', $theatreCate]
            ];
        }
        if (!empty($venue)) {
            $this->indexWhere[] = [
                ['venue', '=', $venue]
            ];
        }


        //查询演出
        try {
            $theatre = (new \app\common\business\Theatre())->getListData();
            $theatreCate = (new \app\common\business\TheatreCate())->getListData();
            $venue = (new \app\common\business\Venue())->getListData();
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }
        $this->assign('theatre', $theatre);
        $this->assign('theatreCate', $theatreCate);
        $this->assign('venue', $venue);
        //查询剧场
        if ($this->request->isAjax()) {
            $limit = $this->request->param('limit', 10, 'intval');

            try {
                $lists = $this->busObj->getIndexLimitPage($limit,$this->indexWhere,'id desc',$this->indexField);
            } catch (\Exception $e) {
                return Show::json_show(Status::status_error, "发生错误");
            }
            return Show::json_show(Status::status_normal, "ok", $lists['data'], $lists['total']);
        }
        return $this->fetch();
    }

    /**
     * @return \think\response\Json|void
     */
    public function write()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id == 0) {
            return Show::json_show(Status::status_error, "订单错误");
        }
        try {
            $result = $this->busObj->write($id);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        if ($result) {
            return Show::json_show(Status::status_normal, "核销成功");
        }
    }
}